package com.ljfiction.book.Dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ljfiction.book.R;

import java.util.Objects;

public class TocunDialog {
    private Context mContext;
    private Dialog mDialog;
    private Listener listener;
    private TextView toptextview;
    private  TextView textViewback;
    private TextView textViewTocun;

    public TocunDialog(Context context) {
        mDialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        @SuppressLint("InflateParams") View view = LayoutInflater.from(context).inflate(R.layout.tocungaodialog, null);
        toptextview=view.findViewById(R.id.tocun_top);
        textViewback = view.findViewById(R.id.tocun_back);
        textViewTocun = view.findViewById(R.id.tocun_baocun);
        textViewback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.back();
                mDialog.dismiss();
            }
        });
        textViewTocun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.cuntocungao();
                mDialog.dismiss();
            }
        });

        //将布局设置给Dialog
        mDialog.setContentView(view);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = context.getResources().getDisplayMetrics().widthPixels;
        view.setLayoutParams(layoutParams);
        Objects.requireNonNull(mDialog.getWindow()).setGravity(Gravity.CENTER);
        mDialog.getWindow().setWindowAnimations(R.style.AnimBottom);
        mDialog.setCancelable(true);
    }

    public void setListener(Listener listener,String toptext,String lefttext,String righttext) {
        this.listener = listener;
        toptextview.setText(toptext);
        textViewback.setText(lefttext);
        textViewTocun.setText(righttext);
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public interface Listener {
        void back();

        void cuntocungao();
    }

}
